package com.dazhewan.project.component;

import com.dazhewan.project.properties.CutScreenInfo;
import com.dazhewan.project.properties.GlobalSettting;
import com.dazhewan.project.properties.ScreenInfoCache;
import com.dazhewan.utils.OcrUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author: zheng
 * @code date: 2022/10/12
 * @code className: ScreenPane
 * @code description:
 */
public class ScreenPane extends JPanel {
    private int startx, starty, endx, endy;
    private CutScreenInfo cutScreenInfo;
    private ScreenFrame screenFrame;

    public ScreenPane(CutScreenInfo cutScreenInfo, ScreenFrame screenFrame) {
        this();
        this.cutScreenInfo = cutScreenInfo;
        this.screenFrame = screenFrame;
    }

    public BufferedImage getBufferedImage() {
        final BufferedImage image = cutScreenInfo.getWindowImage();
        BufferedImage copy= new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = copy.createGraphics();
        g2d.drawImage(image,0,0,null);
        g2d.dispose();
        return copy;
    }

    public ScreenPane() {
        startx = starty = endx = endy = 0;
        MouseAdapter listener = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                setStartPoint(e.getX(), e.getY());
                if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
                    //右键返回并清理内存
                    screenFrame.distroy();
                    return;
                }
                setStartPoint(e.getX(), e.getY());
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
                    setEndPoint(e.getX(), e.getY());
                }
                setEndPoint(e.getX(), e.getY());
                repaint();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
                    //右键返回
                    return;
                }
                setEndPoint(e.getX(), e.getY());
                cutScreenInfo.setImage(startx, starty, endx, endy);
                ScreenInfoCache.init().addScreenInfo(cutScreenInfo);
                screenFrame.distroy();
                repaint();
                GlobalSettting.MAIN_BUILDER.updateCanvas();

                try {
                    String ocrResult = OcrUtil.doOcrTesscat(cutScreenInfo.getImage(),cutScreenInfo.getOperateCode());
                    GlobalSettting.MAIN_BUILDER.updateTextContent(ocrResult);
                } catch ( IOException ex) {
                    ex.printStackTrace();
                }

            }
        };
        addMouseListener(listener);
        addMouseMotionListener(listener);
    }

    public void setStartPoint(int x, int y) {
        this.startx = x;
        this.starty = y;
    }

    public void setEndPoint(int x, int y) {
        this.endx = (x);
        endy = (y);
    }

    public void drawPerfectRect(Graphics g, int x, int y, int x2, int y2) {
        int px = Math.min(x, x2);
        int py = Math.min(y, y2);
        int pw = Math.abs(x - x2);
        int ph = Math.abs(y - y2);
        g.drawRect(px, py, pw, ph);
    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        final BufferedImage bufferedImage = getBufferedImage();
        final Graphics2D graphics = (Graphics2D)bufferedImage.getGraphics();
        graphics.setColor(Color.RED);
        drawPerfectRect(graphics, this.startx, this.starty, this.endx, this.endy);
        g.drawImage(bufferedImage, 0, 0, this);
    }
}





